import pandas, duckdb
from time import strftime
import os
from utils.path_utils import get_project_root
from database.duckdb.duckdb_base import get_connection



# timenow = str(strftime("%Y%m%d%H%M%S"))
def run(dwbh, dwid, zb, kjnd, filepath, timenow):
  excel_data = pandas.read_excel(filepath, sheet_name=None, dtype=str)
  df1 = excel_data.get('项目信息')
  conn = get_connection()
  conn.register('temp_xmzd_main', df1)

  conn.execute("""
               delete from T_API_XM where fiyear = ? and ACCOUNTORG = ?;
               """, [kjnd, dwid])
  conn.execute("""
    insert into T_API_XM (ID, accountorg, cusitemproperty, cusitemcode, cusitemname_chs, fiyear, isdisabled, orgcode, orgname, parentid, timestamp_createdon,
               timestamp_lastchangedon, esun_api_orgid, dwbh, esun_api_update, esun_api_dwbh, parent_treeinfo_isdetail)
              select
                ID,
                '"""+dwid+"""',
                项目类型_名称,
                编码,
                名称,
                '"""+kjnd+"""',
                case 状态_是否可用
                when '是' then 1
                when '否' then 0
                end,
                '"""+dwbh+"""',
                管理组织_名称,
                父节点信息_父节点字段,
                时间戳_创建时间,
                时间戳_最后修改时间,
                '"""+dwid+"""',
                '"""+dwbh+"""',
                '"""+timenow+"""',
                '"""+dwbh+"""',
                case 父节点信息_是否明细
                when '是' then 1
                when '否' then 0
                end,
               from temp_xmzd_main;
  """)

  conn.close()



# run('03047', '01051507', '2024', r'D:\code\app_report_back20241201\会计科目_数据导出.xlsx', timenow)